package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.MonthBillEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

@Implements(MonthBillService.class)
public class MonthBillServiceImpl implements MonthBillService {
	
	@Override
	@Transactional
	public Long createMonthBill(MonthBillEntity entity) {
		entity.setCreateDt(DateUtil.now());
		entity.setUpdateDt(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getMonthBillId();
	}
	
	@Override
	@Transactional
	public void disableMonthBill(Long monthBillId) {
		MonthBillEntity entity = PersistUtil.get(MonthBillEntity.class, monthBillId);
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	public List<MonthBillEntity> selectMonthBillDetail(Integer year,
			Integer month) {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthBillEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (year != null && year > 0) {
			criteria.add(Restrictions.eq("year", year));
		}
		if (month != null && month > 0) {
			criteria.add(Restrictions.eq("month", month));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<MonthBillEntity> selectMonthBillDetailNew() {
		DetachedCriteria criteria = DetachedCriteria.forClass(MonthBillEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.addOrder(Order.desc("createDt"));
		return PersistUtil.get(criteria);
	}
}
